// pages/sharebox/xiche/xiche.js
import {
  getHelpUserInfo,
  helpXiChe
} from '../../../utils/networkRequest.js'
import Net from '../../../utils/network.js'
Page({

  /**
   * 页面的初始数据
   */
  data: {

    statusArr: ["../../../assets/images/zangche.png", "../../../assets/images/xichebg.gif", "../../../assets/images/xinche.png"],
    num: 0,
    dataInfo: null,
    isShow: false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    let self = this;

    var value = wx.getStorageSync('token') //从本地缓存中获取Session信息
    if (!value) {
      wx.login({
        success: function(res) {
          if (res.code) {
            //发起网络请求 
            Net.post('/miniapp.login2', {
              jscode: res.code,
            }, (r) => {
              if (r.code == 0) {
                wx.setStorage({ //将SessionInfo 放入本地缓存
                  key: 'token',
                  data: r.data,
                })
                getHelpUserInfo(options.ppid, (r) => {
                  console.log(r)
                  if (r.code == 0) {
                    if (r.data.isnew) {
                      wx.redirectTo({
                        url: '../newuserxiche/newuserxiche?ppid=' + options.ppid + "&guize=" + r.data.rules,
                      })
                    } else {
                      if (r.data.done){
                        self.setData({
                          dataInfo: r.data,
                          isShow: true,
                          ppid: options.ppid,
                          num:2
                        })
                      } else{
                        if (r.data.has_record) {
                          self.setData({
                            dataInfo: r.data,
                            isShow: true,
                            ppid: options.ppid,
                            num: 1
                          })
                        }else{
                          self.setData({
                            dataInfo: r.data,
                            isShow: true,
                            ppid: options.ppid
                          })
                        } 
                      }
                     
                    }
                  } else {
                    wx.showToast({
                      icon: "none",
                      title: r.message,
                    })
                  }
                })
              }

            }, (e) => {
              console.log(e)
            }, {}, true, '')
            // console.log('获取用户登录态成功！' + res.code)
          } else {
            // console.log('获取用户登录态失败！' + res.errMsg)
          }
        }
      });
    } else {
      getHelpUserInfo(options.ppid, (r) => {
        console.log(r)
        if (r.code == 0) {
          if (r.data.isnew) {
            wx.redirectTo({
              url: '../newuserxiche/newuserxiche?ppid=' + options.ppid,
            })
          } else {
            if (r.data.done) {
              self.setData({
                dataInfo: r.data,
                isShow: true,
                ppid: options.ppid,
                num: 2
              })
            } else {
              if (r.data.has_record) {
                self.setData({
                  dataInfo: r.data,
                  isShow: true,
                  ppid: options.ppid,
                  num: 1
                })
              } else {
                self.setData({
                  dataInfo: r.data,
                  isShow: true,
                  ppid: options.ppid
                })
              }
            }
            // self.setData({
            //   dataInfo: r.data,
            //   isShow: true,
            //   ppid: options.ppid
            // })
          }
        } else {
          wx.showToast({
            icon: "none",
            title: r.message,
          })
        }
      }) 
    } 
  },
  onImg: function() {
     let self = this;
    helpXiChe(this.data.ppid, (r) => {
      debugger;
      if (r.code == 0) {
        wx.showToast({
          icon: "none",
          title: "助力成功",
        })

        if (r.data.done) {
          self.setData({
            dataInfo: r.data, 
            num: 2
          })
        } else {
          if (r.data.has_record) {
            self.setData({
              dataInfo: r.data, 
              num: 1
            })
          } else {
            self.setData({
              dataInfo: r.data,  
            })
          }
        }


        
        
      }else{
        wx.showToast({
          icon:"none",
          title: r.message,
        })
      }
    })

  }
})